package com.hwadee.caixiang.controller;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hwadee.caixiang.entity.User;
import com.hwadee.caixiang.service.ForgotPwdService;
import com.hwadee.caixiang.util.SendmailUtil;
@Controller
public class ForgotPwdController {
	@Autowired
	private ForgotPwdService forgotPwdservice;
	
	@RequestMapping(value = "/forgotPwd", method = RequestMethod.POST)
	public String findPwd(String username,String email,HttpServletRequest request,Model model) {
		User user = forgotPwdservice.forgot(username,email);
		model.addAttribute("username1",username);
		model.addAttribute("email1",email);
		if(user!=null) {
			//return "success";					
			String codes[]= {"NMDHDS","SJAJDD","ZHRMGH","WJJZSH","TLMZQL","WCZQLA","GHSUDN","WXSJJA","WWDDJZ"};
			int num = ((int)(Math.random()*100))%9;
			String temp = codes[num];
			SendmailUtil s= new SendmailUtil(temp);//发送验证码邮件，用构造函数初始化
	    	s.fire();
	    	String emailCode = s.getCode();
	    	model.addAttribute("emailCode1", emailCode);
	    	try {
				s.Sendmail("1693780296@qq.com");
			} catch (AddressException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	    		    	
			//return "redirect:/foundPwd.jsp"; 
			return "redirect:/email.jsp";//views里的jsp												
		}
		else
			return "redirect:/login.jsp";		//重定向是访问WEB-INF外面的jsp
		
	}
	}
//}
//}